<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\KaoQinStatisticsExcelExpoter;
use App\Admin\Renderable\CompanyWorkUser;
use App\Admin\Renderable\KaoQinDetails;
use App\Models\WorkUser;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use App\Admin\Repositories\KaoQinStatisticsApi;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Widgets\Card;

class KaoQinStatisticsController extends AdminController
{

    protected $title = '考勤综合统计';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid($repository = null)
    {
        $grid = new Grid(new KaoQinStatisticsApi());
        $grid->disableCreateButton();

        $grid->filter(function (Grid\Filter $filter) {
            $filter->panel();
            $filter->expand();

            $filter->date('start', '开始时间')->width(5);
            $filter->date('end', '结束时间')->width(5);

            $filter->like('engineering', '所属工程')->select(function ($status){
                $Adminid  = Admin::user()->id;
                if($Adminid != 1){
                    $engineering_name = DB::table('engineering')->where('manager','like','%'.$Adminid.'%')->where('deleted_at',null)->pluck('engineering_name');
                }else{
                    $engineering_name = DB::table('engineering')->where('deleted_at',null)->pluck('engineering_name');
                }
                $arr = [];
                foreach ($engineering_name as $key => $value){
                    $arr["$value"] = $value;
                }
                return $arr;
            })->width(5);

        });
        $grid->disablePagination();
        $grid->export(new KaoQinStatisticsExcelExpoter());

        $grid->number('序号');
        $grid->column('engineering','所属工程');
        $grid->column('name','名称');
        $grid->column('id_card','身份证号');
        $grid->column('type_work','工种');
        $grid->column('basic_salary','基础薪资');
        $grid->column('clock_in_day','打卡天数');
        $grid->column('salary','总薪资');

//        $grid->column('details','明细')->display(function ($details) {
//            return DB::table('admin_users')->where('id',$uploader)->value('username');
//        });

        $grid->column('details','明细')
            ->display('查看') // 设置按钮名称
            ->modal(function ($modal) {
                $card = new Card(null, $this->details);
                return "<div style='padding:10px 10px 0'>$card</div>";
            });


//        $this->start = $grid->model()->filter()->input('start');
//        $this->end = $grid->model()->filter()->input('end');

//        $grid->column(''明细')->modal(function (Grid\Displayers\Modal $modal) {
//            $modal->xl();
//            $modal->title('考勤明细');
//            $modal->icon('feather icon-edit');
//            return KaoQinDetails::make()->payload(['id_card' => $this->id_card]);
//        });


        $grid->disableActions();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableQuickEdit();
            $actions->disableView();
        });


        return $grid;
    }

    protected function form()
    {
        $form = new Form(new InTheater());

        $form->display('id', 'ID');
        $form->text('title')->rules('required');
        $form->text('original_title');
        $form->textarea('summary');
        $form->url('alt');
        $form->url('mobile_url');
        $form->url('share_url');
        $form->tags('countries');
        $form->tags('genres');
        $form->tags('aka');
        $form->year('year');

        $form->disableViewButton();
        $form->disableViewCheck();

        return $form;
    }
}
